<?php
/**
 * SEARCH SUGGESTION
 */
add_action('wp_head','cpx_search_suggest');
function cpx_search_suggest()
{ ?>
<script type="text/javascript">
    
    jQuery(document).ready(function($) {
        
        var si = "#search-input";
        $(si).keyup(function ()
        {   
            // Begin process when enter 3 characters
            var kw_length = $(si).val().length ;
            if(kw_length >= 3)
            {   
                // Append new <div> 
                var div_begin = "<div id='search_suggest'>";
                var div_end = "</div>";
                $("div.search").append(div_begin + div_end);
                ////////////////////////////////////////////////
                var search_keyword = $(si).val();
                
                var data = {
        		action: 'cpx_search_action',
        		vl: search_keyword
                };
                // Process callback 
                //var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';
                
                $.post(ajaxurl, data, function(rs) {
                    var arr_ = rs.split(';');
                    
                    if(arr_.length > 1)
                    {                        
                        var html_rs = '';
                        
                        for(i=0; i< arr_.length; i++)
                        {
                            if(arr_[i] != 0)
                            {
                                var st_detail = arr_[i].split('|');
                                if(st_detail.length > 0)
                                {
                                    var st_logo = st_detail[0];
                                    var st_title = st_detail[1];
                                    var st_link = st_detail[2];
                                    
                                    html_rs += "<div class='search-result'>";
                                    html_rs += "<div class='img-search'><a href='"+st_link+"'><img src='"+st_logo+"' alt='"+st_title+"'/></a></div>";
                                    
                                    html_rs += "<div class='title-search'><h4><a href='" + st_link + "' title='View coupons at " + st_title + "'>" + st_title + "</a></h4></div>";
                                    html_rs += "</div>";
                                }
                            }                            
                        }
                    }                    
                    else
                    {
                        $("#search_suggest").html('');
                    }
                    // Print result below search input
                    if(html_rs)
                    {
                        $("#search_suggest").html(html_rs);
                    }
                })
            }
            else
            {
                $("#search_suggest").html('');
            }
        })
    })
</script>
<?php
}
// Search for public
add_action('wp_ajax_nopriv_cpx_search_action','cpx_search_cb');
// Search for admin
add_action('wp_ajax_cpx_search_action','cpx_search_cb');
function cpx_search_cb()
{   
    $kw = $_POST['vl'];
    $qr = cpx_search_query_store($kw, "LIMIT 0,5");
    global $wpdb;
    $rs = $wpdb->get_results($qr);
    $return_values = '';
    if(count($rs) > 0)
    {
        foreach ($rs as $obj) {
            $st_id = $obj->ID;
            if($st_id > 0)
            {
                $st_link = get_permalink($st_id);
                $st_homepage = get_post_meta($st_id,'url_store_metadata',true);
                $st_title = get_post_field('post_title', $st_id);
                
                $st_logo = get_post_meta($st_id, 'logo_metadata',true);
                if(!$st_logo)
                    $st_logo = cpx_get_store_thumb($st_homepage);
                
                $return_values .= $st_logo.'|'.$st_title.'|'.$st_link.';';
            }
        }
    }    
    echo $return_values;
    die();
}
/**
 * END SEARCH SUGGESTION
 */